﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Common;
using ZLCMS.Data.Model;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using ZLCMS.Business.UploadFileInfo;

namespace ZLCMS.Web.SystemManage.Basics.Location
{
    public partial class CityDetail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        public string uid;
        protected DataCity _city;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                SaveCity();
            }
        }

        protected void InitializeGlobalValue()
        {
            //页面控件值
            if (!IsPostBack)
            {
                ViewState["uid"] = Request["uid"];
            }
                try
                {
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        uid = ViewState["uid"] as string;

                        if (!string.IsNullOrEmpty(uid))
                        {
                            _city = session.Get<DataCity>(int.Parse(uid));
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                }
            
            
        }

        protected void InitializeContent()
        {
            
            //页面控件值
            if (!IsPostBack)
            {
                ParamKit.Bind("Province", ddl_province, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
                if(_city!=null)
                { 
                    txtCode.Text = _city.Code;
                    txtName.Text = _city.Name;
                    ddl_province.SelectedValue = _city.ProvinceCode;
                }
            }
        }

        /// <summary>
        /// 保存数据库
        /// </summary>
        protected void SaveCity()
        {
            ReturnCode rc = new ReturnCode();
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                uid = string.IsNullOrEmpty(ViewState["uid"] as string) ? "0" : (ViewState["uid"] as string);
                int count = Convert.ToInt32(session.CreateQuery("select count(*) from DataCity where id!=:uid and Code=:code ").SetParameter("uid", uid).SetParameter("code", txtCode.Text.Trim()).UniqueResult());
                if (count > 0)
                {
                       rc. statusCode = 300;
                       rc.message = "该编码已存在!";
                    rc.callbackType = "forward";
                    rc.forwardUrl = "/SystemManage/Basics/Location/CityDetail.aspx?tabid=citydetail";
                }
                else
                {
                    _city = session.Get<DataCity>(int.Parse(uid));

                    if (_city == null)
                    {
                        _city = new DataCity();
                    }
                    else
                    {
                        if (_city.Code != txtCode.Text.Trim())
                        {
                            session.CreateQuery("Update DataArea set CityCode=:code1 where CityCode=:code2").SetParameter("code1", txtCode.Text.Trim()).SetParameter("code2", _city.Code).ExecuteUpdate();
                        }
                    }
                    _city.Name = txtName.Text.Trim();
                    _city.Code = txtCode.Text.Trim();
                    _city.ProvinceCode = ddl_province.SelectedValue;

                    session.SaveOrUpdate(_city);
                    session.Flush();


                    rc.statusCode = 200;
                    rc.message = "操作成功!";

                    rc.callbackType = "forward";
                    rc.forwardUrl = "/SystemManage/Basics/Location/CityDetail.aspx?tabid=citydetail&uid=" + _city.Id;

                }

                _framePage.Alert(rc);
            }
        }
    }
}
